<?php

//------------------------------------------------------------------
// File: events.code.php
// Author: Bryan Frost
// Date Created: 2010-03-25
//
// events.php codebehind
//------------------------------------------------------------------
	
require_once('dataAccess.php');
require_once('utility.php');
require_once('../classes/Player.class.php');
require_once('../classes/Event.class.php');
require_once('../classes/Team.class.php');
require_once('../classes/Tournament.class.php');


function GenerateResultsContent()
{
	$tournamentId = $_GET['tournament'];
	$eventId = $_GET['event'];
	
	if (is_numeric($tournamentId))
	{
		GenerateTournamentContent($tournamentId);
	}
	elseif (is_numeric($eventId))
	{
		GenerateEventContent($eventId);
	}
	else
	{
		GenerateAllTournaments();
	}
}

// Tournament ID found
function GenerateTournamentContent($tournamentId)
{
	$tournament = Tournament::GetTournamentData($tournamentId);
	
	$tournamentId =				$tournament->GetTournamentId();
	$tournamentDate =			FormatDate($tournament->GetStartDate());
	$eventsCollection =			$tournament->GetEventsCollection();
	
	$breadCrumb  = "<a href=\"results.php\">Results</a>";
	$breadCrumb .= " - $tournamentDate";
	
	echo '<br />';
	echo '<h3>';
	echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	echo $breadCrumb;
	echo '</h3>';
	echo '<div class="grid">';
	echo '<table cellspacing="0">';
	echo '<col  />';
	echo '<col  />';
	echo '<col  />';
	echo '<col  />';
	echo '<thead>';
	echo '<tr class="headings">';
	echo '<th>Event Format</th>';
	echo '<th>Number of teams</th>';
	echo '<th>Winner</th>';
	echo '</tr>';
	echo '</thead>';
	echo '<tbody>';
	
	$i = 0;
	foreach ($eventsCollection as $key => $value)
	{		
		$eventId = $eventsCollection[$key]->GetEventId();
		$eventFormat = $eventsCollection[$key]->GetEventFormat();
		$eventNumberOfTeams = $eventsCollection[$key]->GetNumberOfTeams();
		
		$eventWinner = "";
		$j = 0;
		
		$playerCollection = $eventsCollection[$key]->GetWinningTeam()->GetPlayerCollection();
		foreach ($playerCollection as $key2 => $value)
		{
			$eventWinner .= $playerCollection[$key2]->GetFullName();
			if ($j == 0)
				$eventWinner .= " & ";
			$j++;
		}
		
		$eventLink = "<a href=\"results.php?event=$eventId\">$eventFormat</a>";

		// Alternate rows use css class "alternate"
		$cssRow = ($i & 1 ? "class=alternate" : "");
		
		echo "<tr $cssRow><td>$eventLink</td>";
		echo "<td>$eventNumberOfTeams</td>";
		echo "<td>$eventWinner</td>";
		echo "</tr>";
		
		$i++;
	}
	echo '</tbody>';
	echo '</table> ';
	echo '</div>';
}

// EventID found
function GenerateEventContent($eventId)
{
	$event = Event::GetEventData($eventId);
	
	$tournamentId =					$event->GetTournamentId();
	$tournamentDate =				FormatDate($event->GetTournamentStartDate());
	$tournamentFormat =				$event->GetEventFormat();
	$numberOfTeams =				$event->GetNumberOfTeams();
	$teamsCollection =				$event->GetTeamsCollection();
	
	
	$genderId =						$event->GetGenderId();
	$divisionId = 					$event->GetDivisionId();
	$division = 					$event->GetDivision();
	
	$breadCrumb  = "<a href=\"results.php\">Results</a>";
	$breadCrumb .= " - <a href=\"results.php?tournament=$tournamentId\">$tournamentDate</a>";
	$breadCrumb .= " - $tournamentFormat";
	
	echo '<br />';
	echo '<h3>';
	echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	echo $breadCrumb;
	echo '</h3>';
	echo '<div class="grid">';
	echo '<table cellspacing="0">';
	echo '<col  />';
	echo '<col  />';
	echo '<col  />';
	echo '<col  />';
	echo '<thead>';
	echo '<tr class="headings">';
	echo '<th>TeamID</th>';
	echo '<th colspan="2">Team</th>';
	echo '<th>Result</th>';
	echo '<th class="last">Earned</th>';
	echo '</tr>';
	echo '</thead>';
	echo '<tbody>';
	
	$i = 0;
	foreach ($teamsCollection as $key => $value)
	{
		$teamId = $teamsCollection[$key]->GetTeamId();
		$teamResult = ConvertIntegerToOrdinal($teamsCollection[$key]->GetConditionalResult());
		
		$ratingEarned = CalculateRatingEarned($genderId, $divisionId, $numberOfTeams, $teamsCollection[$key]->GetConditionalResult());
		
		if ($ratingEarned < 4)
			$ratingEarned = GetRatingName($ratingEarned);
		else
			$ratingEarned = "";
		
		
		// Alternate rows use css class "alternate"
		$cssRow = ($i & 1 ? "class=alternate" : "");
		
		echo "<tr $cssRow><td>$teamId</td>";
		
		$numberOfTeamates = 0;
		
		$playerCollection = $teamsCollection[$key]->GetPlayerCollection();
		foreach($playerCollection as $key2 => $value)
		{
			$playerId = $playerCollection[$key2]->GetPlayerId();
			$firstName = $playerCollection[$key2]->GetFirstName();
			$lastName = $playerCollection[$key2]->GetLastName();

			$numberOfTeamates++;
			if ($numberOfTeamates <= 2 )
			{ 
				echo "<td><a href=playerDetail.php?player=$playerId>$firstName $lastName</a></td>";
			}
			else
			{
				// TODO: Add logic once AVA starts tracking more than 2 person teams.
				// (for now, only display first 2 teamates)
			}
		}
		
		echo "<td>$teamResult</td>";
		echo "<td>$ratingEarned</td>";
		echo "</tr>";
		
		$i++;
	}
	
	echo '</tbody>';
	echo '</table> ';
	echo '</div>';
}


// Show All Tournaments
function GenerateAllTournaments()
{
	$tournamentsDataCollection = Tournament::GetAllTournamentData();
	
	$breadCrumb  = "Results";
	
	echo '<br />';
	echo '<h3>';
	echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	echo $breadCrumb;
	echo '</h3>';
	echo '<div class="grid">';
	echo '<table cellspacing="0">';
	echo '<col  />';
	echo '<col  />';
	echo '<col  />';
	echo '<col  />';
	echo '<thead>';
	echo '<tr class="headings">';
	echo '<th>Tournament</th>';
	echo '<th>Division</th>';
	echo '<th>Teams</th>';
	echo '<th>Winner</th>';
	echo '</tr>';
	echo '</thead>';
	echo '<tbody>';
	
	$i = 0;

	foreach($tournamentsDataCollection as $key => $value)
	{
		$tournamentId =		$tournamentsDataCollection[$key]->GetTournamentId();
		//$tournamentDate =	FormatDate($tournamentsDataCollection[$key]->GetTournamentDate());
		$tournamentDate =	FormatDate($tournamentsDataCollection[$key]->GetStartDate());
		
		$eventsCollection =	$tournamentsDataCollection[$key]->GetEventsCollection();
		$tournamentLink = "<a href=\"results.php?tournament=$tournamentId\">$tournamentDate</a>";
		
		// Alternate rows have unique css class "alternate"
		$cssRow = ($i & 1 ? "alternate" : "");
		
		echo "<tr class=$cssRow>";
		echo "<td>$tournamentLink</td>";
		
		$divisionString = "";
		$teamsString = "";
		$winnerString = "";
		
		$j = 0;
		foreach ($eventsCollection as $key => $value)
		{
			if ($j > 0)
			{
				$divisionString .= "<br />";
				$teamsString .= "<br />";
				$winnerString .= "<br />";
			}
				
			$eventId = $eventsCollection[$key]->GetEventId();
			$eventFormat = $eventsCollection[$key]->GetEventFormat();
			$eventLink = "<a href=\"results.php?event=$eventId\">$eventFormat</a>";
			$divisionString .= "$eventLink";
			
			$teamsString .= $eventsCollection[$key]->GetNumberOfTeams();

			$eventWinner = "";
			$k = 0;
			
			$playerCollection = $eventsCollection[$key]->GetWinningTeam()->GetPlayerCollection();
			foreach ($playerCollection as $key2 => $value)
			{
				$eventWinner .= $playerCollection[$key2]->GetFullName();
				if ($k == 0)
					$eventWinner .= " & ";
					
				$k++;
			}			
			$winnerString .= $eventWinner;
			
			$j++;
		}
		
		echo "<td>$divisionString</td>";
		echo "<td>$teamsString</td>";
		echo "<td>$winnerString</td>";
		echo "</tr>";
		
		$i++;
	}
	
	echo '</tbody>';
	echo '</table> ';
	echo '</div>';
}


// Show all Events
function GenerateAllEvents()
{
	$eventsPreviewCollection = Event::GetAllEventPreviews();
	
	$i = 0;

	foreach($eventsPreviewCollection as $key => $value)
	{
		$event = new Event($row['EventID']);
		
		$eventId =			$eventsPreviewCollection[$key]->GetEventId();
		$tournamentId =		$eventsPreviewCollection[$key]->GetTournamentId();
		$tournamentDate =	FormatDate($eventsPreviewCollection[$key]->GetTournamentDate());
		$divisionId =		$eventsPreviewCollection[$key]->GetDivisionId();
		$division =			$eventsPreviewCollection[$key]->GetDivision();
		$genderId =			$eventsPreviewCollection[$key]->GetGenderId();
		$numberOfTeams =	$eventsPreviewCollection[$key]->GetNumberOfTeams();
		$eventFormat =		$eventsPreviewCollection[$key]->GetEventFormat();
		
		// Alternate rows have unique css class "alternate"
		$cssRow = ($i & 1 ? "alternate" : "");
		
		echo "<tr class=$cssRow>";
		echo "<td><a href=eventDetail.php?event=$eventId>$tournamentDate</a></td>";
		echo "<td>$eventFormat</td>";
		echo "<td>$numberOfTeams</td>";
		echo "</tr>";
		
		$i++;
	}
}
	
?>